package com.example.trackcircuitdiagnosis.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.trackcircuitdiagnosis.entity.*;
import com.example.trackcircuitdiagnosis.service.DeviceInformationService;
import com.example.trackcircuitdiagnosis.service.DeviceService;
import com.example.trackcircuitdiagnosis.service.MonitoringProjectService;
import com.example.trackcircuitdiagnosis.vo.DeviceVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/device")
public class DeviceController {

    @Autowired
    private MonitoringProjectService monitoringProjectService;

    @Autowired
    private DeviceInformationService deviceInformationService;

    @Autowired
    private DeviceService deviceService;

    @GetMapping
    @Operation(description = "查找全部信息")
    public List<DeviceVo> findAll() {
        return monitoringProjectService.getAllMonitoringDevice();

    }



    @GetMapping("/deviceinformation/{mid}")
    @Operation(description = "获取模拟量配置信息",
            parameters = {
                    @Parameter(name = "mid", description = "查找的检测项目id,1是zpw2000a,2是25HZ")
            })
    public List<DeviceInformationEntity> findDeviceInformationAll(@PathVariable Integer mid) {
        return deviceInformationService.findDeviceInformationAll(mid);

    }


    @GetMapping("/deviceALl")
    @Operation(description = "获取设备信息")
    public List<DeviceEntity> findDeviceAll() {
        return deviceService.list();

    }



    @PostMapping
    @Operation(description = "保存设备详细信息")
    public boolean save(@RequestBody DeviceEntity deviceEntity) {
        return deviceService.saveOrUpdate(deviceEntity);
    }

    @DeleteMapping("/{id}")
    @Operation(
            description = "删除设备信息",
            parameters = {
                    @Parameter(name = "id", description = "要删除的设备信息id")
            }

    )
    public Boolean delete(@PathVariable Integer id) {
        return deviceService.removeById(id);
    }

    @PostMapping("/del/batch")
    @Operation(
            description = "批量删除设备",
            parameters = {
                    @Parameter(name = "ids", description = "要批量删除的设备信息")
            }

    )
    public boolean deleteBatch(@RequestBody List<Integer> ids) {
        return deviceService.removeByIds(ids);
    }






    @GetMapping("/device/page")
    @Operation(
            description = "根据mid查询对应的device区段信息",
            parameters = {
                    @Parameter(name = "mid", description = "mid"),
                    @Parameter(name = "pageNum", description = "要查找信息页数"),
                    @Parameter(name = "pageSize", description = "要查找信息每一页的数量")
            }

    )
    public Page<DeviceEntity> findPageByMid(@RequestParam Integer mid,
                                     @RequestParam Integer pageNum,
                                     @RequestParam Integer pageSize) {
        QueryWrapper<DeviceEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("mid",mid);
        queryWrapper.orderByDesc("did");
        Page<DeviceEntity> page = deviceService.page(new Page<>(pageNum, pageSize), queryWrapper);
        return page;
    }



    @GetMapping("/deviceinformation/page")
    @Operation(
            description = "根据did查询对应的deviceinformation区段监控的模拟量信息",
            parameters = {
                    @Parameter(name = "did", description = "did"),
                    @Parameter(name = "pageNum", description = "要查找信息页数"),
                    @Parameter(name = "pageSize", description = "要查找信息每一页的数量")
            }
    )
    public Page<DeviceInformationEntity> findPageByDid(@RequestParam Integer did,
                                       @RequestParam Integer pageNum,
                                       @RequestParam Integer pageSize) {
        QueryWrapper<DeviceInformationEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("did",did);
        queryWrapper.orderByDesc("id");
        return deviceInformationService.page(new Page<>(pageNum, pageSize), queryWrapper);
    }





}



